package com.wwj.core.system.entity;

import com.wwj.framework.entity.BaseIdEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xya on 16-10-24.
 */
public class Menu extends BaseIdEntity<Integer> {

    private static final long serialVersionUID = 2542770916664042797L;

    /**
     * 菜单编码
     */
    private String code;

    /**
     * 菜单名称
     */
    private String name;


    private Integer pid;

    /**
     * 父编码
     */
    private String pcode;
    /**
     * 描述
     */
    private String description;
    /**
     * 页面链接
     */
    private String pageUrl;
    /**
     * 资源类别(0:目录,1:页面)
     */
    private Integer type;
    /**
     * 排序
     */

    private Integer sort = 99;
    /**
     * 图标样式
     */
    private String icon;

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPcode() {
        return pcode;
    }

    public void setPcode(String pcode) {
        this.pcode = pcode;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPageUrl() {
        return pageUrl;
    }

    public void setPageUrl(String pageUrl) {
        this.pageUrl = pageUrl;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public static List<Map<String,Object>> convert2TreeView(List<Menu> menus){
        List<Map<String, Object>> nodeList = new ArrayList<Map<String, Object>>();
        Map<String,Object> state = new HashMap<String,Object>();
        state.put("expanded",false);
        for (Menu menu:menus){
            Map<String, Object> node = new HashMap<String, Object>();
            //List<Map<String,Object>> nodes = new ArrayList<Map<String, Object>>();
            node.put("id", menu.getId());
            node.put("code",menu.getCode());
            node.put("text", menu.getName() +" - " + menu.description);
            node.put("state",state);
            if (!StringUtils.isBlank(menu.getPcode())) {
                node.put("pcode", menu.getPcode().toString());
            } else {
                node.put("pcode", "");
            }
            nodeList.add(node);
        }
        return toTree(nodeList);
    }

    private static List<Map<String, Object>> toTree(List<Map<String, Object>> nodeList) {
        List<Map<String, Object>> _list = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> _temp = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> _list1 = new ArrayList<Map<String, Object>>(nodeList);
        List<Map<String, Object>> _list2 = new ArrayList<Map<String, Object>>(nodeList);

        for (Map<String, Object> map : _list1) {
            String pcode = (String) map.get("pcode");
            if (StringUtils.isBlank(pcode)) {
                _temp.add(map);
                _list.add(map);
            }
        }
        for (Map<String, Object> map : _temp) {
            _list1.remove(map);
        }
        return toTree2(_list, _list2);
    }

    private static List<Map<String, Object>> toTree2(List<Map<String, Object>> mainList, List<Map<String, Object>> list) {
        List<Map<String, Object>> _list1 = new ArrayList<Map<String, Object>>(list);
        List<Map<String, Object>> _temp = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> map : mainList) {
            String code = (String) map.get("code");
            for (Map<String, Object> map2 : list) {
                String pcode = (String) map2.get("pcode");
                if (StringUtils.isNotBlank(pcode) && code.equals(pcode)) {

                    List<Map<String, Object>> childList = null;
                    if (map.get("nodes") != null) {
                        childList = (List<Map<String, Object>>) map.get("nodes");
                    } else {
                        childList = new ArrayList<Map<String, Object>>();
                    }
                    childList.add(map2);
                    map.put("nodes", childList);
                    _temp.add(map2);
                }
            }
            for (Map<String, Object> map3 : _temp) {
                _list1.remove(map3);
            }
            if (map.get("nodes") != null) {
                toTree2((List<Map<String, Object>>) map.get("nodes"), list);
            }
        }
        return mainList;
    }
}
